package com.thinkgem.jeesite.common.listener;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListener;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.springframework.beans.factory.annotation.Autowired;

import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;


public class OnlineUserListener implements SessionListener {

	@Autowired
	private SessionDAO sessionDao;
	
	public int getSessionSize()
	{
		/*Collection<Session> sessions =  sessionDao.getActiveSessions();
		if(null == sessions)
		{
			return 0;
		}
		return sessions.size();*/
		return OnlineCount.getOnlineCount();
	}
	
	@Override
	public void onStart(Session session) {
		System.out.println("session created");
		
		/*User user = UserUtils.getUser();
		System.out.println(user.getLoginName());*/
		
		// session创建时加1
		OnlineCount.increment(1);
		
	}

	@Override
	public void onStop(Session session) {
		// TODO Auto-generated method stub
		System.out.println("session stoped");
		// session退出时减2，因为退出后还要到首页，此时还要走onstart方法加1
		
		
		OnlineCount.reduce(1);
		System.out.println(getSessionSize());
		
	}

	@Override
	public void onExpiration(Session session) {
		// TODO Auto-generated method stub
		System.out.println("session expired");
		OnlineCount.reduce(1);
	}

	/*private static Integer ONLINE_COUNT = 0;
	public static Integer getONLINE_COUNT() {
		return ONLINE_COUNT;
	}
	
	public static void setONLINE_COUNT(Integer oNLINE_COUNT) {
		ONLINE_COUNT = oNLINE_COUNT;
	}*/

}
